package inglar.autotc.client.ui;

import inglar.autotc.client.Utilities;

import java.io.Serializable;


import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.Cookies;
import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
import com.google.gwt.user.client.ui.HasVerticalAlignment;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.ListBox;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.VerticalPanel;

public class DialogBoxSaveOrLoadTemplate extends DialogBox {
	private VerticalPanel contentPanel;
	
	private VerticalPanel savePanel;
	private TextBox txtbxName;
	private Button btnSave;
	private Button btnSaveCancel;
	
	private VerticalPanel loadPanel;
	private ListBox listNames;
	private Button btnLoad;
	private Button btnLoadCancel;
	
	private PageDevices ts = null;
	private TcTemplate[] tct = null;

	private DialogBoxSaveOrLoadTemplate() {
		setGlassEnabled(true);
		setAnimationEnabled(true);
		setHTML("Templates");
		
		contentPanel = new VerticalPanel();
		setWidget(contentPanel);
		contentPanel.setSize("100%", "100%");
		
		savePanel = new VerticalPanel();
		savePanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		txtbxName = new TextBox();
		savePanel.add(txtbxName);
		HorizontalPanel p1 = new HorizontalPanel();
		savePanel.add(p1);
		btnSave = new Button("Save");
		p1.add(btnSave);
		btnSave.getElement().setAttribute("style", "margin-left: 5px; margin-right: 5px;");
		btnSave.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				if(ts.tree.getItemCount() == 0) {
					Window.alert("You don't have a tree.");
					hide();
				} else {
					if(ts.treeSaved) {
						saveTree();
					}
				}
			}
		});
		btnSaveCancel = new Button("Cancel");
		p1.add(btnSaveCancel);
		btnSaveCancel.getElement().setAttribute("style", "margin-left: 5px; margin-right: 5px;");
		btnSaveCancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		});
		
		loadPanel = new VerticalPanel();
		loadPanel.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_CENTER);
		HorizontalPanel p2 = new HorizontalPanel();
		loadPanel.add(p2);
		p2.setVerticalAlignment(HasVerticalAlignment.ALIGN_MIDDLE);
		p2.add(new Label("Load: "));
		listNames = new ListBox();
		p2.add(listNames);
		listNames.getElement().setAttribute("style", "margin-left: 10px");
		HorizontalPanel p3 = new HorizontalPanel();
		loadPanel.add(p3);
		btnLoad = new Button("Load");
		p3.add(btnLoad);
		btnLoad.getElement().setAttribute("style", "margin-left: 5px; margin-right: 5px;");
		btnLoad.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				loadTree();
			}
		});
		btnLoadCancel = new Button("Cancel");
		p3.add(btnLoadCancel);
		btnLoadCancel.getElement().setAttribute("style", "margin-left: 5px; margin-right: 5px;");
		btnLoadCancel.addClickHandler(new ClickHandler() {
			@Override
			public void onClick(ClickEvent event) {
				hide();
			}
		});
	}
	
	public DialogBoxSaveOrLoadTemplate(PageDevices tsRef) {
		this();
		ts = tsRef;
	}
	
	public void save() {
//		Control.showLoadingModal();
		contentPanel.clear();
		contentPanel.add(savePanel);
//		setTemplate();
		center();
	}

	private void saveTree() {
		String userid = Cookies.getCookie("QID");
		final String name = txtbxName.getText().trim();
		if(name.isEmpty()) {
			Window.alert("Enter a name.");
			return;
		} else if(name.contains("#")) {
			Window.alert("Don't put a '#' in the name.");
		} else {
			AsyncCallback<String> setTemplateCallback = new AsyncCallback<String>() {
				@Override
				public void onSuccess(String result) {
					if(!result.isEmpty()) Window.alert(result);
					Utilities.hideLoadingModal();
					hide();
				}
				@Override
				public void onFailure(Throwable caught) {
					Utilities.failureAlert("setTemplateCallback", "RPC failure", caught);
					Utilities.hideLoadingModal();
					hide();
				}
			};
			Utilities.showLoadingModal();
			String cmd = ts._fork(ts.tree.getItem(0));
			Utilities.rpc.setTemplate(userid, ts.currentDevice, name, cmd, setTemplateCallback);
		}
	}
	
	public void load() {
		Utilities.showLoadingModal();
		contentPanel.clear();
		contentPanel.add(loadPanel);
		getTemplateList();
	}
	
	public void getTemplateList() {
		AsyncCallback<String[]> getTemplateListCallback = new AsyncCallback<String[]>() {
			@Override
			public void onSuccess(String[] result) {
				if(result == null || result.length == 0) {
					Window.alert("There are no available templates for this device.");
					Utilities.hideLoadingModal();
					hide();
				} else {
					tct = new TcTemplate[result.length];
					for(int i = 0; i < result.length; i++) {
						String[] t = result[i].split("#");
						if(t.length != 4) {
							Window.alert("result[" + i + "] does not satiate. \n\nt: " + result[i]);
							continue;
						}
						String id = t[0];
						String name = t[1];
						String device = t[2];
						String script = t[3];
						tct[i] = new TcTemplate(id, name, device, script);
					}
					fillData();
					Utilities.hideLoadingModal();
					center();
				}
			}
			@Override
			public void onFailure(Throwable caught) {
				Utilities.hideLoadingModal();
				Utilities.failureAlert("getTemplateListCallback", "RPC failure", caught);
				hide();
			}
		};
		String userid = Cookies.getCookie("QID");
		Utilities.rpc.getTemplateList(userid, ts.currentDevice, getTemplateListCallback);
	}
	
	public void fillData() {
		if(tct == null) {
			return;
		}
		for(int i = 0; i < tct.length; i++) {
			if(tct[i] == null) continue;
			listNames.addItem(tct[i].name);	
		}
	}
	
	private void loadTree() {
		int s = listNames.getSelectedIndex();
		ts.setSettingDetails(tct[s].id, tct[s].name);
		String[] tc = tct[s].script.split(";");
		ts.populateTree(tc, ts.currentDevice);
		
		hide();
	}
	
	protected class TcTemplate implements Serializable {
		
		private static final long serialVersionUID = 2176140632128811248L;
		
		public String id = "";
		public String name = "";
		public String device = "";
		public String script = "";
		
		public TcTemplate(String id, String name, String device, String script) {
			this.id = id;
			this.device = device;
			this.name = name;
			this.script = script;
		}
	}

}
